//
//  VerificationCouponController.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/10/7.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "VerificationCouponController.h"
#import "VerCouponV.h"
#import "VerRecordController.h"
#import "VerResultController.h"
#import "SCScanController.h"
#import "BillViewController.h"
#import  <AVFoundation/AVCaptureDevice.h>
#import <AVFoundation/AVFoundation.h>

@interface VerificationCouponController ()

@property (nonatomic,strong)NSMutableArray *couponArr;

@property (nonatomic,weak)UILabel *couponF;
@property (nonatomic,weak)UIButton *addBtn;
@property (nonatomic,weak)VerCouponV *verCouV;

@property (nonatomic,assign)NSInteger index;

@end

@implementation VerificationCouponController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"核销劵码";
    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        
    }];
    
    
    _index = 0;
    _couponArr = [NSMutableArray array];
    [self setUI];
    [self NavItem];
}
- (void)NavItem{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 80, 32);
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:13];
    [button setTitle:@"扫一扫" forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"sao"] forState:UIControlStateNormal];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, -32-4, 0, 0)];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 48+8, 0,0 )];
    [button addTarget:self action:@selector(codeClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
}
- (void)codeClick{
    [self.view endEditing:YES];
    SCScanController *scanVC = [[SCScanController alloc] init];
    [self.navigationController pushViewController:scanVC animated:YES];
}

- (void)setUI{
    
    
    UIScrollView *mainView = [[UIScrollView alloc]init];
    [self.view addSubview:mainView];
    UIView *containerV = [[UIView alloc]init];
    [mainView addSubview:containerV];

    UIView *couponBgV = [[UIView alloc] init];
    couponBgV.backgroundColor = [UIColor whiteColor];
    [containerV addSubview:couponBgV];
    
    UILabel *couponF = [[UILabel alloc] init];
    couponF.text = @"添加劵码";
    couponF.font = [UIFont systemFontOfSize:14];
    [couponBgV addSubview:couponF];
    _couponF = couponF;
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setTitle:@"继续添加" forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    addBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    addBtn.backgroundColor = KOrange;
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.layer.cornerRadius = 3;
    [couponBgV addSubview:addBtn];
    _addBtn = addBtn;
    
    __weak __typeof(&*self)weakSelf = self;
    VerCouponV *verCouV = [[VerCouponV alloc] initWithFrame:CGRectMake(0, 0, kWidth, 35)data:@[@{@"index":@"0",@"conpou":@""}]] ;
    verCouV.Vclick = ^(NSDictionary *dic) {
        
        NSString *Vindex = dic[@"index"];
        for (int i = 0; i < weakSelf.couponArr.count; i++) {
            NSDictionary *Vdic = weakSelf.couponArr[i];
            if ([Vindex isEqualToString:[NSString stringWithFormat:@"%@",Vdic[@"index"]]]) {
                [weakSelf.couponArr removeObject:Vdic];
                [weakSelf.couponArr insertObject:dic atIndex:i];
            }
        }
    };
    verCouV.DVclick = ^(NSString *index) {
        [weakSelf.couponArr removeObjectAtIndex:[index integerValue]];
        for (int i = 0; i<weakSelf.couponArr.count; i++) {
            NSMutableDictionary *delDic = [NSMutableDictionary dictionaryWithDictionary:weakSelf.couponArr[i]];
            [delDic setValue:[NSString stringWithFormat:@"%@",@(i)] forKey:@"index"];
            [weakSelf.couponArr replaceObjectAtIndex:i withObject:delDic];
        }
        weakSelf.verCouV.data = weakSelf.couponArr;
        [weakSelf.verCouV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(weakSelf.couponArr.count*40);
        }];
    };
    [_couponArr addObject:@{@"index":@"0",@"conpou":@""}];
    [containerV addSubview:verCouV];
    _verCouV = verCouV;
    
    UILabel *detailL = [[UILabel alloc] init];
    detailL.font = [UIFont systemFontOfSize:13];
    detailL.text = @"可使用APP扫描核销";
    [containerV addSubview:detailL];
    
    UIButton *RecordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [RecordBtn setTitle:@"核销记录" forState:UIControlStateNormal];
    [RecordBtn setTitleColor:WYColor(103, 154, 87) forState:UIControlStateNormal];
    [RecordBtn setBackgroundColor:[UIColor whiteColor]];
    RecordBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [RecordBtn addTarget:self action:@selector(RecordClick) forControlEvents:UIControlEventTouchUpInside];
    RecordBtn.layer.cornerRadius = 3;
    RecordBtn.layer.borderWidth = 1;
    RecordBtn.layer.borderColor = WYColor(103, 154, 87).CGColor;
    [containerV addSubview:RecordBtn];
    
    UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [yesBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [yesBtn addTarget:self action:@selector(yesClick) forControlEvents:UIControlEventTouchUpInside];
    yesBtn.layer.cornerRadius = 3;
    [yesBtn setTitle:@"确定" forState:UIControlStateNormal];
    yesBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [yesBtn setBackgroundColor:KOrange];
    [containerV addSubview:yesBtn];
    
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.view);
    }];
    
    [containerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(mainView);
        make.width.equalTo(mainView);
    }];
    [couponBgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(containerV);
        make.top.equalTo(containerV.mas_top).offset(8);
        make.height.equalTo(@40);
    }];
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(couponBgV);
        make.right.equalTo(couponBgV.mas_right).offset(-8);
        make.size.mas_equalTo(CGSizeMake(65, 28));
    }];
    [couponF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(couponBgV);
        make.left.equalTo(couponBgV.mas_left).offset(12);
        make.right.equalTo(addBtn.mas_left).offset(-8);
    }];
    [verCouV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(couponBgV.mas_bottom).offset(8);
        make.left.right.equalTo(containerV);
        make.height.equalTo(@40);
    }];
    [detailL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(verCouV.mas_bottom).offset(30);
        make.left.equalTo(containerV.mas_left).offset(12);
    }];
    [RecordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(detailL.mas_centerY);
        make.right.equalTo(containerV.mas_right).offset(-12);
        make.size.mas_equalTo(CGSizeMake(60, 28));
    }];
    [yesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(RecordBtn.mas_bottom).offset(20);
        make.left.equalTo(containerV.mas_left).offset(18);
        make.right.equalTo(containerV.mas_right).offset(-18);
        make.height.equalTo(@38);
    }];
    [containerV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(yesBtn.mas_bottom).offset(10);
    }];
}


- (void)addClick{
    [self.view endEditing:YES];

    _index = _couponArr.count;
    [_couponArr addObject:@{@"index":[NSString stringWithFormat:@"%@",@(_index)],@"conpou":@""}];
    _verCouV.data = _couponArr;
    __weak __typeof(&*self)weakSelf = self;
    [_verCouV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(weakSelf.couponArr.count*40);
    }];
    
}
- (void)RecordClick{
    [self.view endEditing:YES];

//    VerRecordController *recordVC = [[VerRecordController alloc] init];
//    [self.navigationController pushViewController:recordVC animated:YES];
    
    BillViewController *billVC = [[BillViewController alloc] init];
    [self.navigationController pushViewController:billVC animated:YES];
    
}
- (void)yesClick{
    [self.view endEditing:YES];

    NSMutableArray *orderCodeA = [NSMutableArray array];
    for (NSDictionary *couDic in _couponArr) {
        NSString *code = [NSString stringWithFormat:@"%@",couDic[@"coupon"]];
        if ([code emptyOrWhitespace] || [code isEqualToString:@"(null)"]) {
            [SCTool showError:@"请填写完整"];
            return;
        }
        [orderCodeA addObject:couDic[@"coupon"]];
    }
    [SCTool showLoading:@"正在核销"];
    __weak __typeof(&*self)weakSelf = self;
//    [SCApiTool ScWriteoffOrdercode:@[@"P653728478377",@"P653728478388"] success:^(NSDictionary *dic) {
    [SCApiTool ScWriteoffOrdercode:orderCodeA success:^(NSDictionary *dic) {
    
        [SCTool showMessage:@"核销完毕"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            VerResultController *resultVC = [[VerResultController alloc] init];
            resultVC.listCode = weakSelf.couponArr.count;
            resultVC.listDic = dic;
            [weakSelf.navigationController pushViewController:resultVC animated:YES];

        });
        
    } failure:^(NSString *errormsg) {
        [SCTool showError:errormsg];
    }];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
